package com.api.customization.toU8;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import com.yunzhou.utils.DBTools;
import com.yunzhou.utils.DateTools;
import com.yunzhou.utils.LogTool;
import jxl.read.biff.Record;
import org.junit.Test;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.GCONST;
import weaver.monitor.cache.CacheFactory;
import weaver.system.SysRemindWorkflow;
import weaver.workflow.request.RequestComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Receiver;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * @Author Huangwm
 * @Date 2023-03-05
 * @Version 1.0
 * @Description TODO 更新到货日期并提醒研发物料申请人跟项目经理
 **/
@Path("/oa_u8")
public class UpdateAndRemind {
    private LogTool logTool = new LogTool("/log/toU8/UpdateAndRemind", false);

    private final DateTools dateTools = new DateTools();
    private final DBTools dbTools = new DBTools();

    @Test
    public  static void  main(String args[]) throws Exception {
        String rootPath = "E:\\WEAVER9\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        UpdateAndRemind remind = new UpdateAndRemind();
        Map<String,Object> param = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("801807");
        list.add("801808");
        param.put("requestid",list);
        param.put("dateStr","2023-03-12");
        System.err.println(remind.remind(param));
    }

    @POST
    @Path("/remind")
    @Produces(MediaType.APPLICATION_JSON)
    public  Map<String, Object> remind(Map<String,Object> param) {
        logTool.writeLog(JSONObject.toJSONString(param));
        Map msgMap = new HashMap<>();
        msgMap.put("status",true);
        msgMap.put("msg","更新成功");
        if (param == null ||!param.containsKey("requestid") ||!param.containsKey("dateStr")){
            msgMap.put("status",false);
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return msgMap;
        }

        List<String> list = (List<String>) param.get("requestid");
        String dateStr = param.get("dateStr").toString();
        if(list == null){
            msgMap.put("status",false);
            msgMap.put("msg","requestid参数为空 请检查参数是否完整！");
            msgMap.put("data","");
            return msgMap;
        }


        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {

            for(int i=0;i<list.size();i++){
                String requestid = list.get(i);

                String tbale = dbTools.getBillTableName(requestid);
                String requestname = dbTools.getRequestName(requestid);

                String sql = "select shenqr,xmjll,mainrequestid from "+ tbale +" t1 left join workflow_requestbase t2 on t1.requestid=t2.requestid where t1.requestid="+requestid;
                logTool.writeLog(sql);
                if(rs.execute(sql) && rs.next()){
                    String shenqr = rs.getString("shenqr");
                    String xmjll = rs.getString("xmjll");
                    String mainRequestid = rs.getString("mainrequestid");
                    String mainCreater = "";
                    rs2.execute("select creater from workflow_requestbase where requestid="+(( mainRequestid.isEmpty() || mainRequestid.equals("null") )?"":mainRequestid));
                    if(rs2.next()) mainCreater = rs2.getString("creater");

                    rst.executeUpdate("update "+tbale +" set sjdhrq='"+dateStr+"' where requestid="+requestid);

                    SysRemindWorkflow remind = new SysRemindWorkflow();
                    remind.make("物料已到货提醒["+requestname+"]",
                            0, 0,0, 0, 1,
                            shenqr+","+xmjll+(mainCreater.isEmpty()?"":","+mainCreater),
                            "<span> 该流程物料到货日期已更新为:"+(dateStr.isEmpty()?" 空(ERP弃审) ":dateStr),
                            Integer.parseInt(requestid));
                    //+" <a href='/workflow/request/ViewRequestForwardSPA.jsp?requestid="+requestid+"' target='_blank' >"+requestname+"</a></span>"
//                MessageType messageType = MessageType.newInstance(20);
//                Set<String> userIdList = new HashSet<>(); // 接收人id 必填
//                userIdList.add(shenqr);
//                userIdList.add(xmjll);
//                String title = "到货日期更新["+requestname+"]"; // 标题
//                String context = "该流程到货日期已更新为:"+dateStr+"  "+requestname;// 内容
//                String linkUrl = "/workflow/request/ViewRequestForwardSPA.jsp?requestid="+requestid; // PC端链接
//                String linkMobileUrl = "/spa/workflow/static4mobileform/index.html#/req?requestid="+requestid; // 移动端链接
//                MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
//                messageBean.setCreater(1);// 创建人id
//                //messageBean.setBizState("0");
//                Util_Message.store(messageBean);

                }

            }
            rst.commit();

            CacheFactory.getInstance().clear();
            CacheFactory.getInstance().reset();

            return msgMap;
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            msgMap.put("status",false);
            msgMap.put("msg","OA接口内部异常 "+e.getMessage());
            msgMap.put("data","");
            return msgMap;
        }
    }

    @POST
    @Path("/remind2MC05")
    @Produces(MediaType.APPLICATION_JSON)
    public  Map<String, Object> remind2MC05(Map<String,Object> param) {
        logTool.writeLog(JSONObject.toJSONString(param));
        Map msgMap = new HashMap<>();
        msgMap.put("status",true);
        msgMap.put("msg","更新成功");
        if (param == null ||!param.containsKey("requestid") ||!param.containsKey("dateStr")){
            msgMap.put("status",false);
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return msgMap;
        }

        List<String> list = (List<String>) param.get("requestid");
        String dateStr = param.get("dateStr").toString();
        if(list == null){
            msgMap.put("status",false);
            msgMap.put("msg","requestid参数为空 请检查参数是否完整！");
            msgMap.put("data","");
            return msgMap;
        }


        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {

            for(int i=0;i<list.size();i++){
                String requestid = list.get(i);

                String tbale = dbTools.getBillTableName(requestid);
                String requestname = dbTools.getRequestName(requestid);

                String sql = "select shenqr from "+ tbale +" t1  where t1.requestid="+requestid;
                logTool.writeLog(sql);
                if(rs.execute(sql) && rs.next()){
                    String shenqr = rs.getString("shenqr");

                    rst.executeUpdate("update "+tbale +" set sjdhrq='"+dateStr+"' where requestid="+requestid);

                    SysRemindWorkflow remind = new SysRemindWorkflow();
                    remind.make("物料已采购提醒["+requestname+"]",
                            0, 0,0, 0, 1,
                            shenqr,
                            "<span> 该流程物料采购到货日期已更新为:"+(dateStr.isEmpty()?" 空(ERP弃审) ":dateStr),
                            Integer.parseInt(requestid));
                }

            }
            rst.commit();

            CacheFactory.getInstance().clear();
            CacheFactory.getInstance().reset();

            return msgMap;
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            msgMap.put("status",false);
            msgMap.put("msg","OA接口内部异常 "+e.getMessage());
            msgMap.put("data","");
            return msgMap;
        }
    }

}
